﻿package com.sshc625.core {
	/**
	 * @author sshc625
	 */
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.display.Stage;

	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;

	import com.sshc625.core.IBaseObject;
	import com.sshc625.core.IUIObject;

	public class UIObject extends Sprite implements IUIObject, IBaseObject {

		private var _displayObject:DisplayObject;
		private var _stage:Stage;
		/**
		 * Wrapped a DisplayObject as an UIObject
		 */
		public function UIObject( wrappedDisplayObject:DisplayObject = null ) {
			super();
			this._displayObject = wrappedDisplayObject;
			initialize();
		}

		/**
		 * initialize actions for an UIObject,can be overrided in the subclasses
		 */
		protected function initialize():void {
			if( this._displayObject != null ) {
				addChild( this._displayObject );
				if( this._displayObject is InteractiveObject ) {
					InteractiveObject( this._displayObject ).mouseEnabled = false;
				}
				if( this._displayObject is DisplayObjectContainer ) {
					DisplayObjectContainer( this._displayObject ).mouseChildren = false;
				}
			}
		}
		
		public function setStage(stg:Stage):void {
			this._stage = stg;
		}
		
		public function getStage():Stage {
			return this._stage;
		}
		
		public function moveTo( x:Number, y:Number ):void {
			this.x = x;
			this.y = y;
		}
		public function moveBy(x:Number, y:Number):void {
			this.x += x;
			this.y += y;
		}
	
		/**
		 * hide
		 */
		public function hide( ):void {
			this.visible = false;
		}
		/**
		 * display
		 */
		public function display( ):void {
			this.visible = true;
		}
		
		public function setSize( w:Number, h:Number ):void {
			this.width = w;
			this.height = h;
		}

		/**
		 * change the scaleX,scaleY at the sametime
		 *
		 * @see scaleX,scaleY
		 */
		public function setScale( scale:Number ):void {
			this.scaleX = this.scaleY = scale;
		}

		public function enabled( value:Boolean ):void {
			this.mouseEnabled = value;
			this.mouseChildren = value;
		}

		//apis from the BaseObject
		public function getClassName():String {
			return getQualifiedClassName( this );
		}

		public function getClass():Class {
			return Class( getDefinitionByName( getClassName()));
		}

		public function equals( obj:Object ):Boolean {
			return obj == this;
		}
	}
}